<template>
	<view class="content">
		<u-navbar :placeholder='true' title="跨部门订单/管理" leftIcon="arrow-leftward"
			@leftClick="leftClick('/pages/mine/setting/person/add')">
		</u-navbar>
		<view class="main">
			<view style="background-color: #fff;">
				<!-- 			<view class="form">
					<view class="form-l">
						跨部門打單
					</view>
					<view class="form-r">
						<radio-group v-model="selectedOption">
							<radio color="#645AED" value="yes">是</radio>
							<radio color="#645AED" style="margin-left: 60rpx;" value="no">否</radio>
						</radio-group>

					</view>
				</view> -->
				<view class="cbox" style="padding-top:35 rpx;">
					<u-checkbox-group placement="column" @change="checkAll">
						<u-checkbox :checked="allchecked" activeColor="#645AED" :customStyle="{marginBottom: '8px'}"
							label="全选" name="全选">
						</u-checkbox>
					</u-checkbox-group>
				</view>
				<view class="cbox" style="padding-left: 35rpx;">
					<u-checkbox-group v-model="checkboxitem" placement="column" @change="checkboxitembtn()">
						<u-checkbox v-for="(item, index) in branch" :key="index" activeColor="#645AED"
							:customStyle="{marginBottom: '8px'}" :label="item.departmentname" :name="item.department_id"
							:checked="item.check">
						</u-checkbox>
					</u-checkbox-group>
				</view>
			</view>
		</view>
	</view>
</template>
<script>
	import {
		mapState
	} from 'vuex'
	import {
		getdepartmentinfo
	} from '@/network/api/setting.js'
	export default {
		data() {
			return {
				allchecked: false,
				checkboxitem: [],
				branch: [],
				selectedOption: 'no', // 默认选择"是"
				jurisdiction: []
			}
		},
		computed: {
			...mapState({
				userinfo: state => state.user.userinfo,
			})
		},
		onShow() {
			this.getdepartment()
		},
		methods: {
			checkAll() {
				console.log(this.checkboxitem);
				this.allchecked = !this.allchecked
				if (this.allchecked) {
					this.branch.forEach(item => {
						item.check = true
					})
				} else {
					this.branch.forEach(item => {
						item.check = false
					})
				}
			},
			checkboxitembtn() {
				console.log(this.checkboxitem);

			},
			async getdepartment() {
				let data = {
					company_id: this.userinfo.company_id
				}
				const res = await getdepartmentinfo(data)
				this.branch = res.data.branch.reduce((acc, item) => {
					if (item.department && Array.isArray(item.department)) {
						acc.push(...item.department);
					}
					return acc;
				}, []);
				this.branch.map(item => {
					item.check = false
				})
				console.log(this.branch);

			},
			getDepartmentsWithCheckTrue() {
				return this.branch.filter(item => item.check === true);
			},
			leftClick(url) {
				uni.setStorageSync('jurisdiction', this.getDepartmentsWithCheckTrue())
				uni.navigateBack({
					delta: 1
				})
			}
		}
	}
</script>

<style lang="scss">
	.content {
		background-color: #F5F6F9;
		width: 100%;
		min-height: 100vh;
		position: relative;

		.main {

			// background-color: #fff;
			.form {
				margin: 0 32rpx;
				padding: 32rpx 0;
				display: flex;
				justify-content: space-between;
				align-items: center;
				border-bottom: 1rpx solid #DEDEDE;

				.form-l {
					font-size: 32rpx;
					font-family: PingFang SC;
					font-weight: 500;
					color: #666666;
				}

				.form-r {
					display: flex;
					align-items: center;
					justify-content: space-between;

					// flex-direction: row-reverse;
					// margin-left: 48rpx;
					// flex: 1;
					// float: right;
					.picker {
						display: flex;
						align-items: center;
						justify-content: space-between;

						.uni-input {
							font-size: 32rpx;
							font-family: PingFang SC;
							font-weight: 500;
							color: #BEBEBE;
						}
					}

				}
			}

			.form:last-child {
				border-bottom: none;
			}

			.cbox {
				margin: 0 32rpx;

				.list_box {

					.first,
					.second,
					.third {
						display: flex;
						align-items: center;

						.fir_name {
							margin-left: 10px;
						}
					}

					.second {
						margin-top: 20px;
						margin-left: 20px;
					}

					.third {
						margin-top: 20px;
						margin-left: 40px;
					}
				}

			}
		}

		.open {
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: 500;
			color: #666666;
			margin-top: 22rpx;
			background-color: #fff;
			padding: 32rpx;
			display: flex;
			align-items: center;
			justify-content: space-between;
		}

		.btn {
			position: absolute;
			bottom: 15rpx;
			left: 50%;
			transform: translateX(-50%);
			font-size: 32rpx;
			font-family: PingFang SC;
			font-weight: bold;
			line-height: 80rpx;
			color: #FFFFFF;
			width: 686rpx;
			height: 80rpx;
			text-align: center;
			background: #645AED;
			border-radius: 30rpx;
		}
	}
</style>